package guda.mrpc.core.transport.tcp;

import guda.mrpc.core.codec.RpcDecoder;
import guda.mrpc.core.codec.RpcEncoder;
import guda.mrpc.core.server.Server;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * Created by well on 17/5/23.
 */
public class TcpServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    private Server server;

    public TcpServerChannelInitializer(Server server) {
        this.server = server;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        socketChannel.pipeline()
                .addLast(new IdleStateHandler(0, 5, 0, TimeUnit.SECONDS))
                .addLast(new RpcDecoder(server.getServerConfig().getObjectSerialize()))
                .addLast(new RpcEncoder(server.getServerConfig().getObjectSerialize()))
                .addLast(new TcpServerHandler(server));
    }
}
